﻿// $Id: $
// $DateTime: $
// Copyright © 2008 Tristen Fielding

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Resources;

namespace ResXFileCodeGenerator.Internal
{
    /// <summary>
    /// All the pertinent data that pertains to a given resX file resource.
    /// </summary>
    internal sealed class ResourceData
    {
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="resXNode">An XML Resource node.</param>
        public ResourceData(ResXDataNode resXNode)
        {
            _resXNode = resXNode;
            _resXNode.GetNodePosition();

            string resourceTypeName = resXNode.GetValueTypeName((AssemblyName[]) null);
            ResType = Type.GetType(resourceTypeName);

            if (IsResString)
                ResString = (string) resXNode.GetValue((AssemblyName[]) null);
        }

        /// <summary>
        /// Get the name of the resource
        /// </summary>
        public string ResName
        {
            get { return _resXNode.Name; }
        }

        /// <summary>
        /// Get the type that defines this resource.
        /// </summary>
        public Type ResType { get; private set; }

        /// <summary>
        /// True if this resource is a reference to an embedded file; false otherwise.
        /// </summary>
        public bool IsResFile
        {
            get { return _resXNode.FileRef != null; }
        }

        /// <summary>
        /// True if this resource is a string resource; false otherwise.
        /// </summary>
        public bool IsResString
        {
            get { return ResType == typeof(string) ; }
        }

        /// <summary>
        /// Return the resource string if <see cref="IsResString"/> is true; null otherwise.
        /// </summary>
        public string ResString { get; private set; }

        /// <summary>
        /// Get the location of where this resource is located within the resX file.
        /// </summary>
        public Point ResLocation
        {
            get { return _resXNode.GetNodePosition(); }
        }

        /// <summary>
        /// Get the comment for this resource.
        /// </summary>
        public string ResComment
        {
            get { return _resXNode.Comment; }
        }
        
        /// <summary>
        /// True if we can convert the <see cref="ResName"/> to a valid identifier for the given language.
        /// </summary>
        public bool IsValidIdentifier
        {
            get { return !string.IsNullOrEmpty(IdentifierName); }
        }

        /// <summary>
        /// Return the identifier for this resource if <see cref="IsValidIdentifier"/> is true; null otherwise.
        /// </summary>
        public string IdentifierName { get; set; }

        private readonly ResXDataNode _resXNode;
    }
}